/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.sso.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.admin.vo.AdminVO;
import cn.tedu.mall.common.pojo.sso.authentication.CsmallAuthenticationInfo;
import cn.tedu.mall.common.pojo.ums.vo.UserVO;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.common.utils.JwtTokenUtils;
import cn.tedu.mall.sso.mapper.admin.AdminMapper;
import cn.tedu.mall.sso.mapper.user.UserMapper;
import cn.tedu.mall.sso.pojo.vo.UserInfoVO;
import cn.tedu.mall.sso.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    private static final String JWT_TOKEN_HEAD = "Bearer";

    @Override
    public UserInfoVO userInfo(String authToken) {
        String token = authToken.substring(JWT_TOKEN_HEAD.length());
        log.info("获取token:{}", token);
        CsmallAuthenticationInfo userInfo = jwtTokenUtils.getUserInfo(token);
        String type = userInfo.getUserType();
        Long id = userInfo.getId();
        //准备返回数据UserInfoVO
        UserInfoVO userInfoVO = new UserInfoVO();
        if (type != null && "ADMIN".equals(type)) {
            AdminVO adminVO = adminMapper.findById(id);
            userInfoVO.setUserId(id);
            userInfoVO.setNickname(adminVO.getNickname());
            userInfoVO.setPhone(adminVO.getPhone());
            userInfoVO.setUsername(adminVO.getUsername());
        } else if (type != null && "USER".equals(type)) {
            UserVO userVO = userMapper.findById(id);
            userInfoVO.setUserId(id);
            userInfoVO.setNickname(userVO.getNickname());
            userInfoVO.setPhone(userVO.getPhone());
            userInfoVO.setUsername(userVO.getUsername());
        } else {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "无法获取用户信息");
        }
        return userInfoVO;
    }
}
